package com.huabo.audit.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.oracle.entity.TblNbsjAuditplan;
import com.huabo.audit.oracle.entity.TblNbsjPlanProject;
import com.huabo.audit.oracle.vo.TblNbsjAuditPlanVo;
import com.huabo.audit.service.AttachmentService;
import com.huabo.audit.service.TblAutonoNumberService;
import com.huabo.audit.service.TblNbsjAuditplanService;
import com.huabo.audit.service.TblNbsjPlanProjectService;
import com.huabo.audit.service.TblNbsjProjectService;
import com.huabo.audit.util.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@Api(value = "审计计划管理", tags = {"审计计划管理所有接口"})
@RequestMapping(value = "/auditPlan")
public class PlanManageController {
    /**
     * @description 计划管理列表
     * @author lyz
     * @date 2022/4/15 9:36
     */
    @Autowired
    public TblNbsjAuditplanService tblNbsjAuditplanService;
    
    @Resource
    public TblNbsjPlanProjectService tblNbsjPlanProjectService;
    
    @Resource
    public AttachmentService attachmentService;
    
    @Resource
    public TblNbsjProjectService tblNbsjProjectService;
    
    @Autowired
	public TblAutonoNumberService tblAutonoNumberService;

    @GetMapping("/getAuditPlanPageList")
    @ApiOperation("审计计划管理-计划管理列表分页功能")
    public JsonBean getAuditPlanPageList(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
   		@ApiParam(name="pageNumber",value="分页当前页数",required=false)@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
   		@ApiParam(name="pageSize",value="每页记录数",required=false)@RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
   		@ApiParam(name="planStartDate",value="计划开始时间 格式年-月-日",required=false)@RequestParam(value = "planStartDate", required = false) String planStartDate,
   		@ApiParam(name="planEndDate",value="计划结束时间 格式年-月-日",required=false)@RequestParam(value = "planEndDate", required = false) String planEndDate,
   		TblNbsjAuditPlanVo TblNbsjAuditPlanVo) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjAuditplanService.planManagePageList(token,pageNumber,pageSize,TblNbsjAuditPlanVo,planStartDate,planEndDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }

    /**
     * @description 计划管理详情
     * @author lyz
     * @date 2022/4/15 9:37
     */
    @GetMapping("/getAuditPlanInfo")
    @ApiOperation("审计计划管理-根据计划主键查找计划信息")
    public JsonBean getAuditPlanInfo(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="planid",value="审计计划主键",required=true)@RequestParam(value = "planid", required = true) Integer planid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjAuditplanService.findNbsjAuditPlanDetail(token,planid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }

    /**
     * @description 删除计划管理
     * @author lyz
     * @date 2022/4/15 10:15
     */
    @GetMapping("/deleteAuditPlanByPlanId")
    @ApiOperation("审计计划管理-根据计划主键删除计划信息")
    public JsonBean deleteAuditPlanByPlanId(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="planid",value="审计计划主键",required=true)@RequestParam(value = "planid", required = true) Integer planId) {
        
        try {
			return tblNbsjAuditplanService.deletePlanManageByPlanId(planId, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
    
    @RequestMapping(value = "/mergeAuditPlanInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计计划管理-修改或保存审计计划")
	public JsonBean mergeAuditPlanInfo(HttpServletRequest request,@ApiParam(name="plan",value="审计计划实体",required=true)TblNbsjAuditplan plan,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="planStartTime",value="计划开始时间",required=false) @RequestParam(value = "planStartTime", required = false) String planStartTime,
			 @ApiParam(name="planEndTime",value="计划完成时间",required=false) @RequestParam(value="planEndTime", required = false) String planEndTime,
			 @ApiParam(name="attIds",value="附件主键数组 示例1,2,3,4",required=false) @RequestParam(value="attIds", required = false) String attIds)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblNbsjAuditplanService.mergePlanManageInfo(plan,token,planStartTime,planEndTime,attIds);
			} catch (Exception e) {
				ResponseFormat.retParam(0,1000,e.getMessage());
			}
			return jsonBean;
	}
    
    
    @RequestMapping(value = "/mergePlanProjectManageInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计计划管理-审计计划中计划项目新增或修改")
	public JsonBean mergePlanProjectManageInfo(HttpServletRequest request,@ApiParam(name="project",value="审计计划项目实体",required=true)TblNbsjPlanProject project,
			 @ApiParam(name="plancode",value="审计计划编码",required=false) @RequestParam(value = "plancode", required = false) String plancode,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblNbsjPlanProjectService.mergePlanProjectManageInfo(project, project.getPlanid(),plancode,token);
			} catch (Exception e) {
				ResponseFormat.retParam(0,1000,e.getMessage());
			}
			return jsonBean;
	} 
    
    @RequestMapping(value = "/mergePlanProjectManageInfoList", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
   	@ApiOperation(value = "审计计划管理-审计计划中计划项目新增或修改")
   	public JsonBean mergePlanProjectManageInfoList(HttpServletRequest request,
   			 @ApiParam(name="planId",value="审计计划主键 ，如果主键为空则根据编码新增审计计划信息",required=false) @RequestParam(value = "planId", required = false) String planId,
   			 @ApiParam(name="plancode",value="审计计划编码",required=true) @RequestParam(value = "plancode", required = true) String plancode,
   			 @ApiParam(name="projectListJson",value="审计计划编码",required=true) @RequestParam(value = "projectListJson", required = true) String projectListJson,
   			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
   		 JsonBean jsonBean = null;
   			try {
   			 List<TblNbsjPlanProject> projectList = JSONArray.parseArray(projectListJson,TblNbsjPlanProject.class);
   				jsonBean = this.tblNbsjPlanProjectService.mergePlanProjectManageInfoList(projectList, planId,plancode,token);
   			} catch (Exception e) {
   				e.printStackTrace();
   				ResponseFormat.retParam(0,1000,e.getMessage());
   			}
   			return jsonBean;
   	} 
    
    @RequestMapping(value = "/removePlanProjectInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计计划管理-审计计划中计划项目删除")
	public JsonBean removePlanProjectInfo(HttpServletRequest request,
			 @RequestParam(value = "planprojectid", required = true)@ApiParam(name="planprojectid",value="审计计划项目主键",required=true)Integer planprojectid,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblNbsjPlanProjectService.removePlanProjectInfo(token,planprojectid);
			} catch (Exception e) {
				ResponseFormat.retParam(0,1000,e.getMessage());
			}
			return jsonBean;
	}
    
    @RequestMapping(value = "/getPlanProjectListByPlanId", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	@ApiOperation(value = "审计计划管理，通过计划主键获取计划项目集合")
	public JsonBean getPlanProjectListByPlanId(HttpServletRequest request,
			 @RequestParam(value = "planId", required = true)@ApiParam(name="planId",value="审计计划主键",required=true)Integer planId,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblNbsjPlanProjectService.findPlanProjectListInfoByPlanId(token,planId);
			} catch (Exception e) {
				ResponseFormat.retParam(0,1000,e.getMessage());
			}
			return jsonBean;
	} 
    
    @RequestMapping(value = "/getAuditPlanAttInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计计划管理，根据审计计划主键获取审计计划的附件")
    public JsonBean getAuditPlanAttInfo(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="planId",value="审计计划主键 ",required=true) @RequestParam(value = "planId", required = false) Integer planId) {
    	JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjAuditplanService.getAuditPlanAttInfo(token,planId);
		} catch (Exception e) {
			ResponseFormat.retParam(0,1000,e.getMessage());
		}
		return jsonBean;
    }
    
    /**
     * @description 根据attid删除附件
     * @author lyz
     * @date 2022/4/19 9:04
     */
    @GetMapping("/deleteFileById")
    @ApiOperation(value = "审计计划管理-根据附件主键删除附件接口")
    public R deleteFileById(HttpServletRequest request, HttpServletResponse response,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
    	return this.tblNbsjAuditplanService.removeAttInfoByAttId(token,attId);
    }
    
    
    @RequestMapping(value = "/getAuditPlanViewInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
    @ApiOperation(value = "审计计划管理-审计计划查看分页列表接口")
	public JsonBean getAuditPlanViewInfo(HttpServletRequest request,HttpServletResponse response,
			@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			@ApiParam(name="planName",value="查询条件-计划名称",required=false) @RequestParam(value = "planName", required = false) String planName,
			@ApiParam(name="planYear",value="查询条件-计划年度",required=false) @RequestParam(value = "planYear", required = false) String planYear,
			@ApiParam(name="pageNumber",value="分页当前页数",required=false)@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
	   		@ApiParam(name="pageSize",value="每页记录数",required=false)@RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize
			) {
    	
    	JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjAuditplanService.getAuditPlanViewInfo(token,planName,planYear,pageNumber,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseFormat.retParam(0,1000,e.getMessage());
		}
		return jsonBean;
	}

    @RequestMapping(value = "/getAuditPlanViewDetail", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
    @ApiOperation(value = "审计计划管理-审计计划查看获取审计计划详情")
    public JsonBean getAuditPlanViewDetail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="planid",value="审计计划主键",required=true)@RequestParam(value = "planid", required = true) Integer planid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjAuditplanService.findNbsjAuditPlanViewDetail(token,planid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    


    @RequestMapping(value = "/getAuditFileInfoList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
    @ApiOperation(value = "审计计划管理-审计档案列表页面")
    public JsonBean getAuditFileInfoList(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="pageNumber",value="分页当前页数",required=false)@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
	   		@ApiParam(name="pageSize",value="每页记录数",required=false)@RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
	   		@ApiParam(name="projectName",value="项目名称",required=false)@RequestParam(value = "projectName", required = false) String projectName) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjProjectService.getAuditFileInfoList(token,projectName,pageNumber,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    
    
    @RequestMapping(value = "/getAuditFileDetailTreeInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
    @ApiOperation(value = "审计计划管理-审计档案详情页面获取树形菜单信息")
    public JsonBean getAuditFileDetailTreeInfo(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
	   		@ApiParam(name="projectId",value="项目主键",required=true)@RequestParam(value = "projectId", required = true) Integer projectId) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjProjectService.getAuditFileDetailTreeInfo(token,projectId);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    
    /**
     * 获取自动编码
     */
    @RequestMapping(value = "/code/findAutoNumber", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "获取自动编码")
	public JsonBean findAutoNumber(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="tblName",value="",required=false)@RequestParam(value = "tblName", required = false) String tblName,
			 @ApiParam(name="column",value="",required=false)@RequestParam(value = "column", required = false) String column,
			 @ApiParam(name="orgCol",value="",required=false)@RequestParam(value = "orgCol", required = false) String orgCol,
			 @ApiParam(name="noId",value="",required=false)@RequestParam(value = "noId", required = false) Integer noId)throws Exception{
		 JsonBean jsonBean = null;
			try {
				
				jsonBean = this.tblAutonoNumberService.findFlowNextId(tblName, column, orgCol, noId,null, null, null,token);
				
			} catch (Exception e) {
				ResponseFormat.retParam(0,1000,e.getMessage());
			}
			return jsonBean;
	}
    
}
